/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.datatype;

import java.util.HashMap;
import java.util.Map;

import org.biomoby.client.CentralDigestCachedImpl;
import org.biomoby.shared.MobyDataType;
import org.biomoby.shared.MobyException;

public class MobyTypeCache
{
	public static Map<String, DataTypeNode> nodeMap = new HashMap<String, DataTypeNode>();
	public static DataTypeNode rootNode = new DataTypeNode(null);
	public static CentralDigestCachedImpl worker;
	public static void init() throws MobyException
	{
		MobyDataType[] datatypes = worker.getDataTypes();
		// first iteration
		for (MobyDataType dataType : datatypes)
		{
			if (dataType.getName().startsWith("test"))
			{
				continue;
			}
			// populate all nodes.
			DataTypeNode node = new DataTypeNode(dataType);
			nodeMap.put(dataType.getName(), node);
		}

		// second iteration, establish child-parent relationship.
		for (MobyDataType dataType : datatypes)
		{
			if (dataType.getName().startsWith("test"))
			{
				continue;
			}
			String parentNames[] = dataType.getParentNames();
			if (parentNames.length > 0)
			{
				DataTypeNode childNode = nodeMap.get(dataType.getName());
				DataTypeNode parentNode = nodeMap.get(parentNames[0]);
				parentNode.addChild(childNode);
			}

		}
		for (Map.Entry<String, DataTypeNode> entry : nodeMap.entrySet())
		{
			if (entry.getValue().type.getParentNames().length == 0)
			{
				rootNode.addChild(entry.getValue());
			}
		}
	}
	
	
	
	
	
	public static DataTypeNode getTypeNode(String name)
	{
		return nodeMap.get(name);
	}
}
